<div class="alert-title">锁定层级</div>

<div class="layer-info">
    <table width="100%">
        <tr>
            <td style="width:80px;">用户账号：</td>
            <td>
                <input type="text" name="account" />
                <button class="search">查询</button>
            </td>
        </tr>
        <tr>
            <td>账号类型：</td>
            <td class="grade"> </td>
        </tr>
        <tr>
            <td>用户分层：</td>
            <td>
                <select class="layers"></select>
            </td>
        </tr>
        <tr>
            <td>锁定分层：</td>
            <td>
                <label class="am-radio-inline">
                    <input type="radio" value="1" name="lock"> 锁定
                </label>
                <label class="am-radio-inline">
                    <input type="radio" value="0" name="lock" checked> 未锁定
                </label>
            </td>
        </tr>
    </table>

    <div class="button_line">
        <button class="btn-save common_button admin_button">保存</button>
        <button class="close common_button admin_button">取消</button>
    </div>
</div>

<style type="text/css">
    .layer-info table td {
        text-align: left;
        padding: 8px 5px 8px 5px;
    }
</style>

<script>
    $(".layer-info .close").click(function() {
        $("#lock_layer").remove();
    })
    $(".layer-info .btn-save").click(function() {
        if (!context.center.user) {
            alert2("请选择需要操作的用户", true);
            return;
        }

        startLoading();
        CMD.user_layer(context.center.user.id, $(".layer-info select.layers").val(), 
            $("input:radio[value='1']")[0].checked, function() {
                stopLoading();
                $("#lock_layer").remove();
                if (window._f) _f();
            });
    });
    $(".layer-info .search").click(function() {
        var account = $(".layer-info input[name='account']").val().trim();
        if (!account) return;

        startLoading();
        CMD.find_user_by_name(account, function(r) {
            stopLoading();
            if (Util.isEmpty(r.list)) {
                alert2("用户不存在！", true);
                return;
            }
            context.center.user = r.list[0];
            _fillLayerUserInfo();
        });
    });

    // 填充分层信息
    $(".layer-info select.layers").selected({
        maxHeight: 150,
    });
    Layer.query(undefined, function(r) {
        var arr = Layer.sort(r);
        for (var i in arr) {
            var l = '<option value="' + arr[i].level + '">' + arr[i].name + '</option>';
            $(".layer-info select.layers").append(l);
        }
    });

    function _fillLayerUserInfo() {
        if (context.center.user) {
            $(".layer-info input[name='account']").val(context.center.user.mobile || '');
            if (context.center.user.grade && context.center.user.grade >= 50)
                $(".layer-info .grade").html("管理员");
            else if (context.center.user.grade == 49)
                $(".layer-info .grade").html("代理");
            else
                $(".layer-info .grade").html("普通会员");

            $(".layer-info select.layers").val(context.center.user.layer || 0);
            $(".layer-info select.layers").trigger('changed.selected.amui');

            if (context.center.user.layerLock) {
                $("input:radio[value='0']")[0].checked = false;
                $("input:radio[value='1']")[0].checked = true;
            }
            else {
                $("input:radio[value='0']")[0].checked = true;
                $("input:radio[value='1']")[0].checked = false;
            }
        }
    }
    _fillLayerUserInfo();
</script>